﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

namespace DrawingShips
{
    class SimpleShip : IShip
    {
        private double x = 300;
        private double y = 300;

        public Point centerMass = new Point(50, 50);

        private double deltaX = 0;
        private double deltaY = 0;

        private double rotation = 0;
        private double deltaRotation = 0;

        private double thrust = 0;
        private double mass = 1000;

        private Pen standardPen = new Pen(Brushes.White, 1);

        public double XPosition
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }

        public double YPosition
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }

        public double Rotation
        {
            get
            {
                return rotation;
            }
            set
            {
                rotation = value;
            }
        }

        public double DeltaRotation
        {
            get
            {
                return deltaRotation;
            }
            set
            {
                deltaRotation = value;
            }
        }

        public System.Windows.Controls.Image shipImage
        {
            get
            {
                GeometryGroup shipGroup = new GeometryGroup();

                shipGroup.Children.Add(new LineGeometry(new Point(0, 0), new Point(50, 100)));
                shipGroup.Children.Add(new LineGeometry(new Point(0, 0), new Point(-50, 100)));
                shipGroup.Children.Add(new EllipseGeometry(new Point(0, 50), 1, 1));
                TransformGroup transforms = new TransformGroup();
                transforms.Children.Add(new RotateTransform(rotation));

                shipGroup.Transform = transforms;
                
                DrawingGroup newGroup = new DrawingGroup();
                EllipseGeometry ellipse = new EllipseGeometry(new Point(0, 50), 75, 75);
                ellipse.Transform = transforms;

                newGroup.Children.Add(new GeometryDrawing(Brushes.White, standardPen, shipGroup));
                newGroup.Children.Add(new GeometryDrawing(Brushes.Transparent, new Pen(Brushes.Transparent, 1), ellipse));

                DrawingImage shipImage = new DrawingImage(newGroup);
                
                // Freeze the DrawingImage for performance benefits.
                shipImage.Freeze();
                
                Image finalImage = new Image();
                finalImage.Source = shipImage;

                return finalImage;
            }
        }

        public double XVelocity
        {
            get
            {
                return deltaX;
            }
            set
            {
                deltaX = value;
            }
        }

        public double YVelocity
        {
            get
            {
                return deltaY;
            }
            set
            {
                deltaY = value;
            }
        }

        public double Thrust
        {
            get
            {
                return thrust;
            }
            set
            {
                thrust = value < 10 ? value : 10;
            }
        }

        public double Mass
        {
            get
            {
                return mass;
            }
            set
            {
                mass = value;
            }
        }
    }
}
